package compare;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import algorithms.*;

public class IrisCodeExtractor {

	private String _fileExtension = "";
	private File _sourceImgDir;
	private File _segmentationImgDir;
	private File _irisCodesImgDir;
	private File _softwareDir;

	public IrisCodeExtractor(File sourceImgDir, File segmentationImgDir, File irisCodesImgDir, File softwareDir) {

		this._sourceImgDir		= sourceImgDir;
		this._segmentationImgDir = segmentationImgDir;
		this._irisCodesImgDir 	= irisCodesImgDir;
		this._softwareDir 		= softwareDir;
	}

	public void readSource() throws IOException {
		
		File[] files = _sourceImgDir.listFiles();
		for (File f : files) {
			executeSegmentation(f);
		}
	}

	private void executeSegmentation( File f) throws IOException {
		
		String softwareCaht 	= _softwareDir + "\\caht.exe";
		String softwareWahet 	= _softwareDir + "\\wahet.exe";
		String input 			= " -i " + _sourceImgDir + "\\"+ f.getName();
		String outputCaht 		= " -o " + _segmentationImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_caht_texture." + _fileExtension + " -e";
		String outputWahet 		= " -o " + _segmentationImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_wahet_texture." + _fileExtension + " -e";
		//		String outputCaht 		= " -o " + _segmentationImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_caht_texture." + _fileExtension;
		//		String outputWahet 		= " -o " + _segmentationImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_wahet_texture." + _fileExtension;

		System.out.println(softwareCaht + input + outputCaht);
		// run caht.exe 
		Runtime.getRuntime().exec(softwareCaht + input + outputCaht);

		System.out.println(softwareWahet + input + outputWahet);
		// run wahet.exe
		Runtime.getRuntime().exec(softwareWahet + input + outputWahet);		
	}

	public void readSegmentation() throws IOException {

		File[] files = _segmentationImgDir.listFiles();
		for (File f : files) {
			executeFeatureExtraction(f);
		}
	}

	private void executeFeatureExtraction(File f) throws IOException {
		
		String softwarelg 	= _softwareDir + "\\lg.exe";
		String softwareqsw 	= _softwareDir + "\\qsw.exe";
		String softwareko 	= _softwareDir + "\\ko.exe";
		String softwarecr 	= _softwareDir + "\\cr.exe";
		String softwarecb 	= _softwareDir + "\\cb.exe";
		String softwaredct 	= _softwareDir + "\\dct.exe";

		String input 		= " -i " + _segmentationImgDir + "\\"+ f.getName();

		String outputlg 			= " -o " + _irisCodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_lg_code." + _fileExtension;
		String outputqsw 			= " -o " + _irisCodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_qsw_code." + _fileExtension;
		String outputko				= " -o " + _irisCodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_ko_code." + _fileExtension;
		String outputcr				= " -o " + _irisCodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_cr_code." + _fileExtension;
		String outputcb				= " -o " + _irisCodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_cb_code." + _fileExtension;
		String outputdct 			= " -o " + _irisCodesImgDir + "\\" + f.getName().substring(0,f.getName().length()-4) + "_dct_code." + _fileExtension;

		System.out.println(softwarelg + input + outputlg);
		// run lg.exe 
		Runtime.getRuntime().exec(softwarelg + input + outputlg);

		System.out.println(softwareqsw + input + outputqsw);		
		// run qsw.exe
		Runtime.getRuntime().exec(softwareqsw + input + outputqsw);

		System.out.println(softwareko + input + outputko);
		// run ko.exe
		Runtime.getRuntime().exec(softwareko + input + outputko);

		System.out.println(softwarecr + input + outputcr);
		// run cr.exe
		Runtime.getRuntime().exec(softwarecr + input + outputcr);

		System.out.println(softwarecb + input + outputcb);
		// run cb.exe
		Runtime.getRuntime().exec(softwarecb + input + outputcb);

		System.out.println(softwaredct + input + outputdct);
		// run dct.exe
		Runtime.getRuntime().exec(softwaredct + input + outputdct);
	}

	public String getFileExtension() {
		return _fileExtension;
	}

	public void setFileExtension(String fileExtension) {
		_fileExtension = fileExtension;
	}

	public void setInputFormat(String string) {

	}

	public void setOutputFormat(String string) {

	}

	public void binarize(BilevelAlgorithm algorithm) throws IOException {
		File[] files = _irisCodesImgDir.listFiles();
		for (File f : files) {
			BufferedImage bf = algorithm.toBilevelImage(f);
			ImageIO.write(bf, "bmp", f);
		}
	}

}
